<?php
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
include 'config.php';
require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'OPENID')
->setCellValue('B1', '微信昵称')
->setCellValue('C1', '姓名')
->setCellValue('D1', '手机号')
->setCellValue('E1', '省')
->setCellValue('F1', '市')
->setCellValue('G1', '区')
->setCellValue('H1', '注册时间')
->setCellValue('I1', '一等奖')
->setCellValue('J1', '二等奖')
->setCellValue('K1', '三等奖')
->setCellValue('L1', '四等奖')
->setCellValue('M1', '五等奖')
;
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
$sql="SELECT openid FROM ling order by jname";
$query =$mysqli->query($sql);
$lingUser=array();
while ($list=$query->fetch_array()){
	$lingUser[$list['openid']]=$list['openid'];
}



$index=2;
$jT=array(1=>'珠峰纯净之旅', 2=>'品牌液晶电视机', 3=>'空气净化器',4=>'高尔夫雨伞',5=>'精美礼品');
foreach ($lingUser as $k => $users){
  
	$sql="SELECT nickname FROM `users` where openid='".$users."' limit 0,1 ";
	$queryName =$mysqli->query($sql);
	$nameInfo=$queryName->fetch_array();
	
	$sql="SELECT * FROM `uinfo` where openid='".$users."' limit 0,1 ";
	$queryUser =$mysqli->query($sql);
	$userInfo=$queryUser->fetch_assoc();
	$j1=$j2=$j3=$j4=$j5='';
	$sql="SELECT * FROM `ling` where openid='".$users."' ";
	$queryJ =$mysqli->query($sql);
	while ($listJ = $queryJ->fetch_array()){
		if (1 == $listJ['jname']){
			$j1=$jT[$listJ['jname']];
		}
		if (2 == $listJ['jname']){
			 
			$j2=$jT[$listJ['jname']];
		}
		if (3 == $listJ['jname']){
			 
			$j3=$jT[$listJ['jname']];
		}
		if (4 == $listJ['jname']){
			 
			$j4=$jT[$listJ['jname']];
		}
		if (5 == $listJ['jname']){
 
			$j5=$jT[$listJ['jname']];
		}
	}

	$objPHPExcel->setActiveSheetIndex(0)
	->setCellValue('A'.$index, $users)
	->setCellValue('B'.$index, base64_decode($nameInfo['nickname']))
	->setCellValue('C'.$index, $userInfo['name'])
	->setCellValue('D'.$index, $userInfo['phone'])
	->setCellValue('E'.$index, $userInfo['sheng'])
	->setCellValue('F'.$index, $userInfo['shi'])
	->setCellValue('G'.$index, $userInfo['qu'])
	->setCellValue('H'.$index, $userInfo['createtime'])
	->setCellValue('I'.$index, $j1)
	->setCellValue('J'.$index, $j2)
	->setCellValue('K'.$index, $j3)
	->setCellValue('L'.$index, $j4)
	->setCellValue('M'.$index, $j5)
	;
	$index++;
}
 
$sql="SELECT * FROM users ";
$query =$mysqli->query($sql);
while ($list=$query->fetch_array()){
	if (!in_array($list['openid'], $lingUser)){
		$sql="SELECT * FROM `uinfo` where openid='".$list['openid']."' limit 0,1 ";
		$queryUser =$mysqli->query($sql);
		$userInfo=$queryUser->fetch_assoc();
		$j1=$j2=$j3=$j4=$j5='';
		$sql="SELECT * FROM `ling` where openid='".$list['openid']."' ";
		$queryJ =$mysqli->query($sql);
		while ($listJ = $queryJ->fetch_array()){
			if (1 == $listJ['jname']){
				$j1=$jT[$listJ['jname']];
			}
			if (2 == $listJ['jname']){
		
				$j2=$jT[$listJ['jname']];
			}
			if (3 == $listJ['jname']){
		
				$j3=$jT[$listJ['jname']];
			}
			if (4 == $listJ['jname']){
		
				$j4=$jT[$listJ['jname']];
			}
			if (5 == $listJ['jname']){
		
				$j5=$jT[$listJ['jname']];
			}
		}
		
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A'.$index, $list['openid'])
		->setCellValue('B'.$index, base64_decode($list['nickname']))
		->setCellValue('C'.$index, $userInfo['name'])
		->setCellValue('D'.$index, $userInfo['phone'])
		->setCellValue('E'.$index, $userInfo['sheng'])
		->setCellValue('F'.$index, $userInfo['shi'])
		->setCellValue('G'.$index, $userInfo['qu'])
		->setCellValue('H'.$index, $userInfo['createtime'])
		->setCellValue('I'.$index, $j1)
		->setCellValue('J'.$index, $j2)
		->setCellValue('K'.$index, $j3)
		->setCellValue('L'.$index, $j4)
		->setCellValue('M'.$index, $j5)
		;
		$index++;
	}
	 

}






// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户信息');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;